﻿using System;
using System.IO;
using System.Net.Security;
using Deer.Log;
using Letter.Net.Tcp;

namespace Telex;

public sealed class ServerBuilder : ABuilder<ServerBuilder, IRpcServer>
{
    private SslServerConfig sslServerConfig;
    
    public ServerBuilder ConfigureSslServerOptions(SslServerOptions sslServerOptions, Func<Stream, SslStream> sslServerStreamFactory)
    {
        if (sslServerOptions == null)
        {
            throw new ArgumentNullException(nameof(sslServerOptions));
        }

        if (sslServerStreamFactory == null)
        {
            throw new ArgumentNullException(nameof(sslServerStreamFactory));
        }

        this.sslServerConfig = new SslServerConfig();
        this.sslServerConfig.sslServerOptions = sslServerOptions;
        this.sslServerConfig.sslServerStreamFactory = sslServerStreamFactory;
        
        return this;
    }
    
    public override IRpcServer Build()
    {
        if (logger == null)
        {
            logger = new DefaultLogger();
        }
        
        return new RpcServer(this.logger, sslServerConfig, secretkey, protocolMap, onChannelActive, onChannelInactive);
    }
}

